'use strict';

const {db, table, Op} = require('./init');

function convertArrayToObj(arr) {
    let res = {}
    for (const iterator of arr) {
        Object.assign(res,iterator);
    }

    return res;
}

/**
 * 根据参数的类型向数据表中插入数据
 * 
 * @param {array[obj] | object} data 
 * @returns modelsArray | models
 */
db.insert = async(table, data) => {
    if (Array.isArray(data)) {
        return await table.blukCreate(data);
    } else if (typeof data === 'object') {
        return await table.create(data);
    } else {
        console.error('Invalid argus');
    }
}

/**
 * 
 * @param {table} table 被查找的表
 * @param {object} options 选项，比如 attributes
 * @returns json object
 */
db.select = async(table,...options) => {
    options = convertArrayToObj(options);
    return await table.findAll(options);
}

module.exports = {
    db,
    table,
    Op
}
